package Cain.keyboardEvent
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	/**
	 * 秘技觸發 上上下下左右左右BA
	 * @author Cain
	 */
	public class UpUpDown extends EventDispatcher
	{
		/**
		 * 秘技啟動事件
		 */
		static public const UPUPDOWN_COMPLETE:String = "UpUpDown_UPUPDOWN_COMPLETE";
		private var UPUPDOWN_ARR:Array = new Array(38, 38, 40, 40, 37, 39, 37, 39, 66, 65);
		private var swich_off:Boolean;
		private var _root:Stage;
		private var _id:int;
		private var me:DisplayObject;
		/**
		 * 秘技實體
		 */
		public function UpUpDown() 
		{
			
		}
		/**
		 * 秘技開關
		 */
		public function set swich(_off:Boolean):void 
		{
			swich_off = _off;
			if (swich_off&&_root!=null) 
			{
				_root.addEventListener(KeyboardEvent.KEY_UP, onKEY_UP);
			}else if(!swich_off&&_root!=null) 
			{
				_root.removeEventListener(KeyboardEvent.KEY_UP, onKEY_UP);
			}
		}
		/**
		 * 秘技開關
		 */
		public function get swich():Boolean 
		{
			return swich_off;
		}
		/**
		 * 秘技初始化 且開關打開
		 */
		public function setup(_mc:DisplayObject):void 
		{
			_root = _mc.stage;
			swich = true;
			_mc.addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
		}
		
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			_root.removeEventListener(KeyboardEvent.KEY_UP, onKEY_UP);
		}
		private function onKEY_UP(e:KeyboardEvent):void 
		{
			if (e.keyCode == UPUPDOWN_ARR[_id]) 
			{
				_id++
			}else 
			{
				_id = 0;
			}
			if (_id==UPUPDOWN_ARR.length) 
			{
				_id = 0;
				dispatchEvent(new Event(UPUPDOWN_COMPLETE));
			}
		}
	}

}